//
//  YHY_MyPageMVViewController.m
//  TTMusic
//
//  Created by yanghuiyan on 16/4/1.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "YHY_MyPageMVViewController.h"
#import "YHYLoadTwoTableViewCell.h"
#import "YHYMVloadTableViewCell.h"
#import "TTMusicNSURLSession.h"
@interface YHY_MyPageMVViewController ()<UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource, NSURLSessionDelegate>


@property (nonatomic, strong) UIScrollView *musicScrollView;

@property (nonatomic, strong) UISegmentedControl *segment;

@property (nonatomic, strong) TTMusicBaseTableView *mvTableView;

@property (nonatomic, strong) TTMusicBaseTableView *mvLoadTableView;


@property (nonatomic, strong) TTMusicNSURLSession *URLSession;

//@property (nonatomic, strong) UIView *customView;

@property (nonatomic, assign) BOOL isDownload;
/**
*  下载任务
*/
@property (nonatomic, strong) NSURLSessionDownloadTask* downloadTask;
/**
 *  resumeData记录下载位置
 */
@property (nonatomic, strong) NSData* resumeData;
/**
 *  session
 */
@property (nonatomic, strong) NSURLSession* session;

@property (nonatomic, strong) UILabel *pgLabel;

@end



@implementation YHY_MyPageMVViewController

+ (instancetype)shareMVPlayViewController  {
    
    static YHY_MyPageMVViewController *MVplayViewController = nil;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        MVplayViewController = [[YHY_MyPageMVViewController alloc] init];
        [MVplayViewController creatView];
    });
    return MVplayViewController;
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        
        
        self.isDownload = YES;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)creatView {
    
    self.view.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    
    //  创建UIScrollView
    
    self.musicScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    _musicScrollView.contentSize = CGSizeMake(SysWidth * 2, SysHeight);
    _musicScrollView.contentOffset = CGPointMake(0, 0);
    _musicScrollView.delegate = self;
    _musicScrollView.pagingEnabled = YES;
    _musicScrollView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:_musicScrollView];
    
    //  创建segment
    self.segment = [[UISegmentedControl alloc] initWithItems:@[@"已下载", @"正在下载"]];
    _segment.frame = CGRectMake(SysWidth / 4, 20 * FitHeight, SysWidth / 2, 40  * FitHeight);
    _segment.tintColor = [UIColor whiteColor];
    
    _segment.selectedSegmentIndex = 0;
    [_segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = _segment;
    
    // 创建tableView
    self.mvTableView = [[TTMusicBaseTableView alloc] initWithFrame:CGRectMake(0, 0, self.musicScrollView.frame.size.width, self.musicScrollView.frame.size.height - 114) style:UITableViewStylePlain];
    [self.mvTableView registerClass:[YHYLoadTwoTableViewCell class] forCellReuseIdentifier:@"YHYLoadTwoTableViewCellIdentifier"];
    
    self.mvTableView.delegate = self;
    self.mvTableView.dataSource = self;
    [self.musicScrollView addSubview:self.mvTableView];
    
    self.mvLoadTableView = [[TTMusicBaseTableView alloc] initWithFrame:CGRectMake(self.musicScrollView.bounds.size.width, 0, self.musicScrollView.frame.size.width, self.musicScrollView.frame.size.height - 114) style:UITableViewStylePlain];
    [self.mvLoadTableView registerClass:[YHYMVloadTableViewCell class] forCellReuseIdentifier:@"YHYMVloadTableViewCellIdentifier"];
    self.mvLoadTableView.delegate = self;
    self.mvLoadTableView.dataSource = self;
    [self.musicScrollView addSubview:_mvLoadTableView];
    
}

#pragma mark - segment控制scrollView
- (void)segmentAction:(UISegmentedControl *)sender {
    
    self.musicScrollView.contentOffset = CGPointMake(SysWidth * sender.selectedSegmentIndex, 0);
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (self.musicScrollView.contentOffset.x == 0) {
        self.segment.selectedSegmentIndex = 0;
    }else if(self.musicScrollView.contentOffset.x == SysWidth) {
        self.segment.selectedSegmentIndex = 1;
    }
}


#pragma mark - tableView代理方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == _mvTableView) {
        YHYLoadTwoTableViewCell *cell = [self.mvTableView dequeueReusableCellWithIdentifier:@"YHYLoadTwoTableViewCellIdentifier"];
        return cell;

    }else {
        YHYMVloadTableViewCell *cell = [self.mvLoadTableView dequeueReusableCellWithIdentifier:@"YHYMVloadTableViewCellIdentifier"];
        NSDictionary *dic = _textArray[indexPath.row];
        NSString *url = [dic objectForKey:@"url"];
        NSString *title = [dic objectForKey:@"title"];
        STLoadModel *model = [[STLLoadMv sharedInstance] getSessionModel:[[STLLoadMv sharedInstance] getTask:url].taskIdentifier];
        model.progressBlock = ^(CGFloat progress) {
            dispatch_async(dispatch_get_main_queue(), ^{
                
                cell.myPregress.progress = progress / 100;
                cell.pgLabel.text = [NSString stringWithFormat:@"%f", progress / 100];
            });
        };
        cell.titleLabel.text = title;
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(observe) userInfo:nil repeats:YES];
        return cell;
    }
}
- (void)observe {
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == _mvLoadTableView) {
        if ( _textArray != 0) {
            return self.textArray.count;
        }else {
            return 0;
        }
    } else {
        return 10;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dic = _textArray[indexPath.row];
    NSString *url = [dic objectForKey:@"url"];
    if (self.isDownload == YES) {
        
        [[STLLoadMv sharedInstance] download:url progress:^(CGFloat progress) {
            
        } state:^(DownloadState DownloadStateStop) {
            
        }];
        self.isDownload = NO;
        
    }else {
        
        [[STLLoadMv sharedInstance] download:url progress:^(CGFloat progress) {
            
        } state:^(DownloadState DownloadStateStart) {
            
        }];
        
        self.isDownload = YES;
        
    }
}
- (void)setTextArray:(NSMutableArray *)textArray {
    _textArray = textArray;
    [_mvLoadTableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
